home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / plotting / gnuplot3.lzh / gnuplot / docs / doc2ms.c < prev    next >
C/C++ Source or Header  |  1991-08-27  |  5KB  |  243 lines

  1. /*
  2.  * doc2ms.c  -- program to convert Gnuplot .DOC format to *roff -ms document
  3.  * From hlp2ms by Thomas Williams 
  4.  *
  5.  * Modified by Russell Lang, 2nd October 1989
  6.  * to make vms help level 1 and 2 create the same ms section level.
  7.  *
  8.  * Modified to become doc2ms by David Kotz (David.Kotz@Dartmouth.edu) 12/89
  9.  * Added table and backquote support.
  10.  *
  11.  * usage:  doc2ms < file.doc > file.ms
  12.  *
  13.  *   where file.doc is a VMS .DOC file, and file.ms will be a [nt]roff
  14.  *     document suitable for printing with nroff -ms or troff -ms
  15.  *
  16.  * typical usage for GNUPLOT:
  17.  *
  18.  *   doc2ms < gnuplot.doc | troff -ms
  19.  */
  20.  
  21. static char rcsid[] = "$Id: doc2ms.c,v 1.1 90/01/11 15:43:54 dfk Exp Locker: dfk $";
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #ifdef AMIGA_LC_5_1
  26. #include <string.h>
  27. #endif
  28.  
  29. #define MAX_NAME_LEN    256
  30. #define MAX_LINE_LEN    256
  31. #define LINE_SKIP        3
  32.  
  33. #define TRUE 1
  34. #define FALSE 0
  35.  
  36. typedef int boolean;
  37.  
  38. static boolean intable = FALSE;
  39.  
  40. main()
  41. {
  42.     init(stdout);
  43.     convert(stdin,stdout);
  44.     finish(stdout);
  45.     exit(0);
  46. }
  47.  
  48.  
  49. init(b)
  50. FILE *b;
  51. {
  52.     /* in nroff, increase line length by 8 and don't adjust lines */
  53.     (void) fputs(".if n \\{.nr LL +8m\n.na \\}\n",b);
  54.     (void) fputs(".nr PO +0.3i\n",b);
  55.     (void) fputs(".so titlepage.ms\n",b);
  56.     (void) fputs(".pn 1\n",b);
  57.     (void) fputs(".bp\n",b);
  58.     (void) fputs(".ta 1.5i 3.0i 4.5i 6.0i 7.5i\n",b);
  59.     (void) fputs("\\&\n.sp 3\n.PP\n",b);
  60.     /* following line commented out by rjl
  61.       (void) fputs(".so intro\n",b);
  62.       */
  63. }
  64.  
  65.  
  66. convert(a,b)
  67.     FILE *a,*b;
  68. {
  69.     static char line[MAX_LINE_LEN];
  70.  
  71.     while (fgets(line,MAX_LINE_LEN,a)) {
  72.        process_line(line, b);
  73.     }
  74. }
  75.  
  76. process_line(line, b)
  77.     char *line;
  78.     FILE *b;
  79. {
  80.     switch(line[0]) {        /* control character */
  81.        case '?': {            /* interactive help entry */
  82.           break;            /* ignore */
  83.        }
  84.        case '@': {            /* start/end table */
  85.           if (intable) {
  86.              (void) fputs(".TE\n", b);
  87.              (void) fputs(".EQ\ndelim off\n.EN\n\n",b);
  88.              intable = FALSE;
  89.           } else {
  90.              (void) fputs("\n.EQ\ndelim $$\n.EN\n",b);
  91.              (void) fputs(".TS\ncenter box tab (@) ;\n", b);
  92.              (void) fputs("c c l .\n", b);
  93.              intable = TRUE;
  94.           }
  95.           /* ignore rest of line */
  96.           break;
  97.        }
  98.        case '#': {            /* latex table entry */
  99.           break;            /* ignore */
  100.        }
  101.        case '%': {            /* troff table entry */
  102.           if (intable)
  103.             (void) fputs(line+1, b); /* copy directly */
  104.           else
  105.             fprintf(stderr, "error: % line found outside of table\n");
  106.           break;
  107.        }
  108.        case '\n':            /* empty text line */
  109.        case ' ': {            /* normal text line */
  110.           if (intable)
  111.             break;        /* ignore while in table */
  112.           switch(line[1]) {
  113.              case ' ': {
  114.                 /* verbatim mode */
  115.                 fputs(".br\n",b); 
  116.                 fputs(line+1,b); 
  117.                 fputs(".br\n",b);
  118.                 break;
  119.              }
  120.              case '\'': {
  121.                 fputs("\\&",b);
  122.                 putms(line+1,b); 
  123.                 break;
  124.              }
  125.              default: {
  126.                 if (line[0] == '\n')
  127.                   putms(line,b); /* handle totally blank line */
  128.                 else
  129.                   putms(line+1,b);
  130.                 break;
  131.              }
  132.              break;
  133.           }
  134.           break;
  135.        }
  136.        default: {
  137.           if (isdigit(line[0])) { /* start of section */
  138.              if (!intable)    /* ignore while in table */
  139.                section(line, b);
  140.           } else
  141.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  142.                   line[0]);
  143.           break;
  144.        }
  145.     }
  146. }
  147.  
  148.  
  149. /* process a line with a digit control char */
  150. /* starts a new [sub]section */
  151.  
  152. section(line, b)
  153.     char *line;
  154.     FILE *b;
  155. {
  156.     static char string[MAX_LINE_LEN];
  157.     int sh_i;
  158.     static int old = 1;
  159.  
  160.   
  161. #ifdef AMIGA_LC_5_1
  162.     (void) sscanf(line,"%d",&sh_i);
  163.     strcpy(string,strchr(line,' ')+1);
  164.     {
  165.       char *p;
  166.       p = strchr(string,'\n');
  167.       if (p != NULL) *p = '\0';
  168.     }
  169. #else
  170.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  171. #endif
  172.     
  173.     (void) fprintf(b,".sp %d\n",(sh_i == 1) ? LINE_SKIP : LINE_SKIP-1);
  174.     
  175.     if (sh_i > old) {
  176.        do
  177.         if (old!=1)    /* this line added by rjl */
  178.           (void) fputs(".RS\n.IP\n",b);
  179.        while (++old < sh_i);
  180.     }
  181.     else if (sh_i < old) {
  182.        do
  183.                if (sh_i!=1) /* this line added by rjl */
  184.                 (void) fputs(".RE\n.br\n",b);
  185.        while (--old > sh_i);
  186.     }
  187.     
  188.     /* added by dfk to capitalize section headers */
  189.     if (islower(string[0]))
  190.      string[0] = toupper(string[0]);
  191.     
  192.     /* next 3 lines added by rjl */
  193.     if (sh_i!=1) 
  194.      (void) fprintf(b,".NH %d\n%s\n.sp 1\n.LP\n",sh_i-1,string);
  195.     else 
  196.      (void) fprintf(b,".NH %d\n%s\n.sp 1\n.LP\n",sh_i,string);
  197.     old = sh_i;
  198.     
  199.     (void) fputs(".XS\n",b);
  200.     (void) fputs(string,b);
  201.     (void) fputs("\n.XE\n",b);
  202. }
  203.  
  204. putms(s, file)
  205.     char *s;
  206.     FILE *file;
  207. {
  208.     static boolean inquote = FALSE;
  209.  
  210.     while (*s != '\0') {
  211.        switch (*s) {
  212.           case '`': {        /* backquote -> boldface */
  213.              if (inquote) {
  214.                 fputs("\\fR", file);
  215.                 inquote = FALSE;
  216.              } else {
  217.                 fputs("\\fB", file);
  218.                 inquote = TRUE;
  219.              }
  220.              break;
  221.           }
  222.           case '\\': {        /* backslash */
  223.              fputs("\\\\", file);
  224.              break;
  225.           }
  226.           default: {
  227.              fputc(*s, file);
  228.              break;
  229.           }
  230.        }
  231.        s++;
  232.     }
  233. }
  234.  
  235. finish(b)        /* spit out table of contents */
  236. FILE *b;
  237. {
  238.     (void) fputs(".pn 1\n",b);
  239.     (void) fputs(".ds RH %\n",b);
  240.     (void) fputs(".af % i\n",b);
  241.     (void) fputs(".bp\n.PX\n",b);
  242. }
  243.